import { message } from 'antd';
import { resolveOnChange } from 'antd/lib/input/Input';
import jsonp from 'jsonp';
import ajax from './ajax';
//登录
export const reqLogin = ( username, password ) => ajax( '/api/login', { username, password }, 'POST' );

// //添加用户
// export const reqAddUser = ( user ) => ajax( '/manage/user/add', user, 'POST' );

// export const reqWeather = (city) => { 
// 	const url = `http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p49MVra6urFRGOT9s8UBWr2`
// 	jsonp( url, {}, ( err, data ) => {
// 		if ( !err && data.status === 'success' ) {
// 			const { dayPictureUrl, weather } = data.results[ 0 ].weather_data[ 0 ];
// 			resolveOnChange( { dayPictureUrl, weather } );
// 		} else { 
// 			message.error('获取天气失败！')
// 		}
// 	 })
// };


//品类管理
//获取分类列表
export const reqCategorys = ( parentId ) => ajax( '/api/manage/category/list', {parentId})

//添加
export const reqAddCategory = (categoryName, parentId ) => ajax( '/api/manage/category/add', {categoryName,parentId},'POST')

//更新
export const reqUpdateCategory = ( { categoryId,categoryName} ) => ajax( '/api/manage/category/update', {categoryId,categoryName},'POST')

export const reqDeleteImg = ( name ) => ajax( '/api/manage/img/delete', { name }, 'POST' );


//添加修改商品
export const reqAddOrUpdateProduct = (product) => ajax('/api/manage/product/' + ( product._id?'update':'add'), product, 'POST')


//获取商品分页列表
export const reqProducts = ( pageNum, pageSize ) => ajax( '/api/manage/product/list', { pageNum, pageSize } );


//搜索商品分页列表
//searchType搜索类型
export const reqSearchProducts = ( { pageNum, pageSize, searchName, searchType } ) => ajax( '/api/manage/product/search', {
	pageNum,
	pageSize,
	[searchType]:searchName
} );

//获取一个分类
export const reqCategory = ( categoryId ) => ajax( '/api/manage/category/info', { categoryId } );


//更新商品状态
export const reqUpdateStatus = ( productId, status ) => ajax( '/api/manage/product/updateStatus', { productId, status }, 'POST' );


//添加角色
export const reqAddRole = ( roleName ) => ajax( '/api/manage/role/add', { roleName }, 'POST' );


//获取所有角色的列表
export const reqRoles = () => ajax( '/api/manage/role/list' )

//更新角色
export const reqUpdateRole = ( role ) => ajax( '/api/manage/role/update', role, "POST" );

//获取用户列表
export const reqUsers = () => ajax( '/api/manage/user/list' );

//删除用户
export const reqDeleteUser = ( userId ) => ajax( '/api/manage/user/delete', { userId }, "POST" );

//添加用户
export const reqAddUser = ( user ) => ajax( '/api/manage/user/add', user, "POST" );


export const reqAddOrUpdateUser = (user) => ajax('/api/manage/user/'+(user._id ? 'update' : 'add'), user, 'POST')